/*
 * Copyright 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package simple.runtime.components.impl.android;

import simple.runtime.android.MainActivity;
import simple.runtime.components.组件;
import simple.runtime.components.impl.android.util.ViewUtil;
import simple.runtime.components.线性布局;

import android.widget.RadioGroup;

/**
 * 用于水平或垂直放置组件的线性布局。
 *
 * @author Herbert Czymontek
 * @author 东子 xhwsd@qq.com
 */
public final class 线性布局Impl extends 布局Impl implements 线性布局 {

  // 内容对齐属性支持
  protected int justification;

  /**
   * 创建一个新的线性布局。
   *
   * @param container  视图容器
   */
  线性布局Impl(视图组件容器 container) {
    // RadioGroup https://developer.android.google.cn/reference/android/widget/RadioGroup
    // 单选组布局（android.widget.RadioGroup），该布局是继承至线性布局（android.widget.LinearLayout）
    // 使用该布局而未直接使用线性布局的原因是为了让Simple中的线性布局兼容单选框情况。
    super(new RadioGroup(MainActivity.getContext()), container);

    //android.widget.LinearLayout layoutManager = (android.widget.LinearLayout) getLayoutManager();
    //layoutManager.setWeightSum(1.0f);

    // 如果不关闭基线对齐，我们就不能让容器只包含其它容器...
    //layoutManager.setBaselineAligned(false);

    // 默认属性值，这里设置属性是因为无法在布局接口中设置初始属性值。
    方向(组件.布局_方向_垂直);
  }

  @Override
  public void addComponent(视图组件 component) {
    /*
    float weight = 0.5f;
    getLayoutManager().addView(component.getView(), new android.widget.LinearLayout.LayoutParams(
        android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
        android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
        weight));
    */
    // RadioGroup.LayoutParams
    //  https://developer.android.google.cn/reference/android/widget/RadioGroup.LayoutParams
    getLayoutManager().addView(component.getView(), new RadioGroup.LayoutParams(
        RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT));
  }

  /**
   * 设置子组件相当于布局的对齐方式。
   *
   * @param component 组件
   * @param justification 对齐
   */
  public void setComponentJustification(视图组件 component, int justification) {
    ((RadioGroup.LayoutParams) component.getView().getLayoutParams()).gravity =
        ViewUtil.simpleToAndroidGravity(justification);
    component.getView().requestLayout(); 
  }

  /**
   * 取子组件的权重。
   *
   * @param component 组件
   * @return 权重
   */
  public float getComponentWeight(视图组件 component) {
    return ((RadioGroup.LayoutParams) component.getView().getLayoutParams()).weight;
  }

  /**
   * 设置子组件的权重。
   *
   * @param component 组件
   * @param weight 权重
   */
  public void setComponentWeight(视图组件 component, float weight) {
    ((RadioGroup.LayoutParams) component.getView().getLayoutParams()).weight = weight;
    component.getView().requestLayout(); 
  }

  // 线性布局 实现

  @Override
  public int 方向() {
    return ((RadioGroup) getLayoutManager()).getOrientation() == RadioGroup.HORIZONTAL ?
        组件.布局_方向_水平 : 组件.布局_方向_垂直;
  }

  @Override
  public void 方向(int newOrientation) {
    ((RadioGroup) getLayoutManager()).setOrientation(newOrientation == 组件.布局_方向_水平 ?
        RadioGroup.HORIZONTAL : RadioGroup.VERTICAL);
  }

  @Override
  public int 内容对齐() {
    return justification;
  }

  @Override
  public void 内容对齐(int justification) {
    // 注意这里是设置其内容（子视图）相对自身的对齐方式
    this.justification = justification;
    ((RadioGroup) getLayoutManager()).setGravity(ViewUtil.simpleToAndroidGravity(justification));
  }

  @Override
  public boolean 基线对齐() {
    return ((RadioGroup) getLayoutManager()).isBaselineAligned();
  }

  @Override
  public void 基线对齐(boolean baselineAligned) {
    ((RadioGroup) getLayoutManager()).setBaselineAligned(baselineAligned);
  }

  @Override
  public float 权重总和() {
    return ((RadioGroup) getLayoutManager()).getWeightSum();
  }

  @Override
  public void 权重总和(float weight) {
    ((RadioGroup) getLayoutManager()).setWeightSum(weight);
  }
}
